
var store = Ext.create('Ext.data.Store', {
    fields: ["billCode","ladingBillCode","mscBillCode",
        "shipperName","containerOwner","boxTypeConfirm","appointEstimateShipTime",
        "appointShipRoute","estimateTime","goodsType",
        "goodsNum","unit","goodsGrossWeight","","volume","tradeType","createTime","salerName",
        "forwarderCustomerService","customsNum","tradeMethod","chineseName",
        "riverPort","riverUnload","riverDelivery",
        "estimateRiverBoatTime","shippingMark","isBookingCargo","isPutBox","isLoadBox","isCustoms","isSignBill","isPreMatchRiver"
        ,"isRiverStowage","isDraftBill","isLadingBill","isCostCompleted","isCheckedAccount","isInvoiceTicket","isReceipt","box_REQUIREMENT","ID"
    ],
    id:'store',
    pageSize: 20,  //页容量5条数据
    //是否在服务端排序 （true的话，在客户端就不能排序）
    remoteSort: false,
    remoteFilter: true,
    proxy: {
        type: 'ajax',
        url: '../bookingCargo/list',
        reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
            type: 'json', //返回数据类型为json格式
            root: 'result.list',  //数据
            totalProperty: 'result.total' //数据总条数
        }
    },
//				sorters: [{
//					//排序字段。
//					property: 'ordeId',
//					//排序类型，默认为 ASC
//					direction: 'desc'
//				}],
    autoLoad: true  //即时加载数据
});

var grid = Ext.create('Ext.grid.Panel', {
    //renderTo: "output",
    id:'grid',
    store: store,
//				height: "100%",
    height: document.documentElement.clientHeight,
//				width: 800,
    stripeRows:true,
    columnLines: true,
    selModel:Ext.create('Ext.selection.CheckboxModel',{mode:"single", allowDeselect: true, showHeaderCheckbox:false}),
    columns: [
        {text: 'ID', dataIndex: 'ID', align: 'left',hidden: true},    
        {text: '托单编号', dataIndex: 'billCode', align: 'left', locked: true, resizable:true},
        {text: '提单号', dataIndex: 'ladingBillCode', locked:true, resizable:true},
//					{text: '海船装区', dataIndex: 'loadingGoingShips', align: 'left', minWidth: 80},
//					{text: '订舱状态', dataIndex: 'bookingStatus', maxWidth: 80, align: 'left'},
        {text: 'msc提单号', dataIndex: 'mscBillCode',  resizable:true},
        {text: '托运人', dataIndex: 'shipperName', resizable:true},
//					{text: '订舱人员', dataIndex: 'bookingClerk', maxWidth: 80},
        {text: '箱主', dataIndex: 'containerOwner', resizable:true},
        {text: '箱型箱量', dataIndex: 'boxTypeConfirm', resizable:true},
        {text: '指定预计海船期', dataIndex: 'appointEstimateShipTime', resizable:true},
        {text: '指定海船航线', dataIndex: 'appointShipRoute', resizable:true},
//					{text: '订舱海船航线', dataIndex: 'bookingShippingRoutes', maxWidth: 80},
//					{text: '订舱海船期', dataIndex: 'bookingSeagoingVessels', maxWidth: 80},
        {text: '预计放箱时间', dataIndex: 'estimateTime', resizable:true, renderer:convertDate},
        {text: '货物种类', dataIndex: 'goodsType', resizable:true},
        {text: '件数', dataIndex: 'goodsNum', resizable:true},
        {text: '放箱要求', dataIndex: 'box_REQUIREMENT', resizable:true},
        {text: '包装类别', dataIndex: 'unit', resizable:true},
        {text: '货物毛重', dataIndex: 'goodsGrossWeight', resizable:true},
        {text: '箱货总重', dataIndex: '', resizable:true},
        {text: '总体积', dataIndex: 'volume', resizable:true},
        {text: '贸易类型', dataIndex: 'tradeType', resizable:true},
        {text: '接单时间', dataIndex: 'createTime', resizable:true, renderer:convertDate},
        {text: '业务员', dataIndex: 'salerName', resizable:true},
        {text: '货代客服', dataIndex: 'forwarderCustomerService', resizable:true},
//					{text: '运输条款', dataIndex: 'transportClause', maxWidth: 80},
        {text: '报关票数', dataIndex: 'customsNum', resizable:true},
        {text: '贸易方式', dataIndex: 'tradeMethod', resizable:true},
        {text: '中文品名', dataIndex: 'chineseName', resizable:true},
        {text: '装运港', dataIndex: 'riverPort', resizable:true},
//					{text: '订舱卸货港', dataIndex: 'bookingDischargingPort', maxWidth: 80},
//					{text: '订舱交货地', dataIndex: 'bookingPlaceDelivery', maxWidth: 80},
        {text: '卸货港', dataIndex: 'riverUnload', resizable:true},
        {text: '交货地', dataIndex: 'riverDelivery', resizable:true},
        {text: '预计江船装船时间', dataIndex: 'estimateRiverBoatTime', resizable:true, renderer:convertDate},
//					{text: '订舱备注', dataIndex: 'bookingOperationNotes', maxWidth: 80},
//					{text: '海船周次', dataIndex: 'sailingTimes', maxWidth: 80},
        {text: '唛头', dataIndex: 'shippingMark', resizable:true},
        {text: '订舱', dataIndex: 'isBookingCargo', resizable:true , renderer :statusCheckBox},
        {text: '放箱', dataIndex: 'isPutBox', resizable:true, renderer:statusCheckBox},
        {text: '装箱', dataIndex: 'isLoadBox', resizable:true , renderer:statusCheckBox},
        {text: '报关', dataIndex: 'isCustoms', resizable:true , renderer:statusCheckBox},
        {text: '签单', dataIndex: 'isSignBill', resizable:true , renderer:statusCheckBox},
        {text: '江船预配', dataIndex: 'isPreMatchRiver', resizable:true , renderer:statusCheckBox},
        {text: '江船实配', dataIndex: 'isRiverStowage', resizable:true , renderer:statusCheckBox},
        {text: '草单', dataIndex: 'isDraftBill', resizable:true , renderer:statusCheckBox},
        {text: '提单', dataIndex: 'isLadingBill', resizable:true , renderer:statusCheckBox},
        {text: '费用完成', dataIndex: 'isCostCompleted', resizable:true , renderer:statusCheckBox},
        {text: '对帐', dataIndex: 'isCheckedAccount', resizable:true , renderer:statusCheckBox},
        {text: '开票', dataIndex: 'isInvoiceTicket', resizable:true , renderer:statusCheckBox},
        {text: '收款', dataIndex: 'isReceipt', resizable:true , renderer:statusCheckBox}
    ],
    bbar: [{
        xtype: 'pagingtoolbar',
        store: store,
        displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
        emptyMsg: "没有数据",
        beforePageText: "当前页",
        afterPageText: "共{0}页",
        displayInfo: true
    }],
    listeners: {
        'itemclick': function (view, record, item, index, e) {
//                    Ext.MessageBox.alert("标题", record.data.cataId);
        }
    },
    tbar: [
        {text: '查询', icon:'../plugins/websocketInstantMsg/ext4/shared/icons/fam/application_view_list.png', handler: toSelect}, "-",
        {text: '新建托单', icon: '../plugins/websocketInstantMsg/ext4/shared/icons/fam/add.png',handler: function(){
        	document.location.href="../consignMgr/consignInput";
        }}, "-",
        {text: '订舱导出', icon: '../plugins/websocketInstantMsg/ext4/shared/icons/fam/cog_edit.png',handler: function(){exportFile("bookingcargo");} }, "-",
        {text: '放箱导入', icon: '../plugins/websocketInstantMsg/ext4/shared/icons/fam/table_refresh.png', handler: toImport}, "-",
        {text: '放箱导出', icon: '../plugins/websocketInstantMsg/ext4/shared/icons/fam/cog_edit.png', handler: function(){exportFile("putbox");}}, "-",
        {width:250,fieldLabel:'放箱要求',labelWidth:80,id:'BOX_REQUIREMENT',xtype:'textfield',store:store,emptyText:'请输入放箱要求'},Ext.create("Ext.Button", {
   	     text: '修改',
   	     handler: function(){   	    	
   	    	 if(Ext.getCmp('grid').getSelectionModel().getSelection().length == 0){
   	    		 Ext.Msg.alert("提示", "请选择托单记录！");
   	    		 return;
   	    	 }
   	    	 var BOX_REQUIREMENT=Ext.getCmp('BOX_REQUIREMENT').getValue();
   	    	 if(BOX_REQUIREMENT==null||BOX_REQUIREMENT==""){
   	    		 Ext.Msg.alert("提示", "请填写放箱要求！");
   	    		 return;
   	    	 }
   	    	 var ID=Ext.getCmp('grid').getSelectionModel().getSelection()[0].get("ID");
   	    	 var store = Ext.getCmp('grid').getStore();
   	    		Ext.Ajax.request({
   					url:'../bookingCargo/update',
   					params:{
   						ID : ID,
   						BOX_REQUIREMENT : BOX_REQUIREMENT,
   						
   					},
   					timeout:5000,
   					success:function(response){
   						Ext.Msg.alert("提示","成功!");
   						store.load();
   					},
   				
   				});
   	    	
   	     }
             
             })
        ]
});


function exportFile(type){

    if(Ext.getCmp('grid').getSelectionModel().getSelection().length == 0){
        Ext.MessageBox.alert("提示", "请选择托单记录！");
        return;
    }
    var billCode = Ext.getCmp('grid').getSelectionModel().getSelection()[0].get("billCode");
    //var store = Ext.getCmp('grid').getStore();	
    if(type == 'bookingcargo'){
        var tradeType = Ext.getCmp('grid').getSelectionModel().getSelection()[0].get("tradeType");
    
        if(tradeType.indexOf("出口")>-1||tradeType.indexOf("进口")>-1){
        	// type += "-" + tradeType;
        	type += "-"+"AZ";
        	 document.location.href="../common/export/exportExcel?exportType="+type+"&billCode="+billCode;
        
        }else{
        	type += "-"+"AK";
       	document.location.href="../common/export/exportExcel?exportType="+type+"&billCode="+billCode;
   
       }
       
    }else if(type == 'putbox'){
    	var isBookingCargo = Ext.getCmp('grid').getSelectionModel().getSelection()[0].get("isBookingCargo");
    	//alert(isBookingCargo)
    	if(isBookingCargo==1){
    		document.location.href="../common/export/exportExcel?exportType="+type+"&billCode="+billCode;
    		
    	}else{
    		 Ext.MessageBox.alert("提示", "该记录没有订舱无法导出！");
    		 return;
    	}

    	
    }
   
    
}


function convertDate(value) {
    var dateStr = Ext.util.Format.date(new Date(value), 'Y-m-d H:i:s');
    if(dateStr != "0NaN-NaN-NaN NaN:NaN:NaN"){
        return dateStr;
    }
    return "";
}


function statusCheckBox(value) {
    return '<input disabled="disabled" type="checkbox"' + (value == "1" ? " checked" : "") + '/>';
}


/*********************修改开始*************************/
var win = null;
function toModify(){

    if(Ext.getCmp('grid').getSelectionModel().getSelection().length == 0){
        Ext.MessageBox.alert("提示", "请选择托单记录！");
        return;
    }
    var billCode = Ext.getCmp('grid').getSelectionModel().getSelection()[0].get("billCode");
    var html = '<iframe src="../bookingCargo/forward?url=/business/bookingcargo/bookingcargo_modify&billCode='+billCode+'"  width="100%"  height="100%"></iframe>';
    var myPanel = new Ext.Panel({
        layout : 'fit',
        html : html,
        frame : false
    });
    win = new Ext.Window({
        width : "90%",
        height :"100%",
        draggable: false,
        resizable:false,
        modal : true,
        layout : 'fit',
        items : [myPanel ]
    });
    win.show();
}



/*********************查询开始*************************/
function toSelect(){
    selWin.show();
}

var statusStore = Ext.create('Ext.data.Store', {
    fields: ['id', 'name'],
    data : [
        {"id":"", "name":"全部"},
        {"id":"1", "name":"未订舱"},
        {"id":"2", "name":"已订舱"},
        {"id":"3", "name":"已放箱"}
    ]
});

var tradeTypeStore = Ext.create('Ext.data.Store', {
    fields: ['id', 'name'],
    data : [
        {"id":"", "name":"全部"},
        {"id":"AK", "name":"内贸"},
        {"id":"AZ", "name":"外贸"}
    ]
});

var businessTypeStore = Ext.create('Ext.data.Store', {
    fields: ['id', 'name'],
    data : [
        {"id":"", "name":"全部"},
        {"id":"1", "name":"出口"},
        {"id":"2", "name":"进口"},
        {"id":"3", "name":"沿江下水"},
        {"id":"4", "name":"沿江上水"},
        {"id":"5", "name":"沿海下水"},
        {"id":"6", "name":"沿海上水"},
        {"id":"7", "name":"其他"}
    ]
});

var c1 = Ext.create('Ext.container.Container', {
    layout: 'column',
    defaults: { labelAlign: 'left', labelWidth: 65 },
    items: [
        {xtype: 'combobox',id:'status', name: 'status', columnWidth: .5,padding: '10px', fieldLabel: '托单状态',value:'',editable: false,displayField: 'name', queryMode: 'local',store:statusStore, valueField: 'id'},
        {xtype: 'textfield',id:'billCode', name: 'billCode',columnWidth: .5,padding: '10px',fieldLabel: '托单编号'}
    ]
});
var c2 = Ext.create('Ext.container.Container', {
    layout: 'column',
    defaults: { labelAlign: 'left', labelWidth: 65 },
    items: [
        {xtype: 'textfield', name: 'ladingBillCode', columnWidth: .5,padding: '10px', fieldLabel: '提单号'},
        {xtype: 'textfield', name: 'containerOwner',columnWidth: .5,padding: '10px',fieldLabel: '箱主'}
    ]
});
var c3 = Ext.create('Ext.container.Container', {
    layout: 'column',
    defaults: { labelAlign: 'left', labelWidth: 65 },
    items: [
        {xtype: 'combobox', name: 'businessType', columnWidth: .5,padding: '10px', fieldLabel: '业务类型',value:'',editable: false,displayField: 'name', queryMode: 'local',store:businessTypeStore, valueField: 'id'},
        {xtype: 'textfield', name: 'riverDelivery',columnWidth: .5,padding: '10px',fieldLabel: '交货地'}
    ]
});
var c4 = Ext.create('Ext.container.Container', {
    layout: 'column',
    defaults: { labelAlign: 'left', labelWidth: 65 },
    items: [
        {xtype: 'textfield', name: 'riverUnload', columnWidth: .5,padding: '10px', fieldLabel: '卸货港'},
        {xtype: 'textfield', name: 'riverPort',columnWidth: .5,padding: '10px',fieldLabel: '起运港区'}
    ]
});
var c5 = Ext.create('Ext.container.Container', {
    layout: 'column',
    defaults: { labelAlign: 'left', labelWidth: 65 },
    items: [
        {xtype: 'textfield', name: 'forwarderCustomerService', columnWidth: .5,padding: '10px', fieldLabel: '货代客服'},
        {xtype: 'combobox', name: 'tradeType',columnWidth: .5,padding: '10px',fieldLabel: '贸易类型',value:'',editable: false,displayField: 'name', queryMode: 'local',store:tradeTypeStore, valueField: 'id'}
    ]
});
var c6 = Ext.create('Ext.container.Container', {
    layout: 'column',
    defaults: { labelAlign: 'left', labelWidth: 65 },
    items: [
        {xtype: 'datefield', name: 'startDate', columnWidth: .5,padding: '10px', fieldLabel: '订舱时间起'},
        {xtype: 'datefield', name: 'endDate',columnWidth: .5,padding: '10px',fieldLabel: '订舱时间止'}
    ]
});
/*var c7 = Ext.create('Ext.container.Container', {
    layout: 'column',
    defaults: { labelAlign: 'left', labelWidth: 60 },
    items: [
        {xtype: 'textfield', name: 'startDate1', columnWidth: .5,padding: '10px', fieldLabel: '订舱人'},
        {xtype: 'combobox', name: 'startDate2',columnWidth: .5,padding: '10px',fieldLabel: '海船装区'}
    ]
});*/

var selForm = Ext.create('Ext.form.Panel', {
    name: 'selForm',
    bodyStyle: {
        background: '#DFE9F6',
        padding: '5px',
        border: 0
    },
    fieldDefaults: {
        labelAlign: 'left'
    },
    items: [c1, c2, c3, c4, c5, c6]
});

var selWin = Ext.create('Ext.window.Window', {
    title: '查询',
    width: 650,
    height: 350,
    modal : true,
    items: [selForm],
    closeAction: 'hide',
    closeable: true,
    iconCls: 'modalWin',
    buttonAlign: 'center',
    buttons: [
        {text: '查询',handler: function() { formSubmit(); }},
        {text: '清空',handler: function() { selForm.form.reset(); }},
        {text: '关闭', handler: function() { selWin.close(); }}
    ]
});

function formSubmit(){
    store.load({ params: selForm.form.getValues()});
    selWin.close();
}

/*********************查询结束*************************/

var uploadForm = Ext.create('Ext.form.Panel', {
    items: [
        {
            xtype: 'filefield',
            fieldLabel: '文件上传',
            name:"file",
            labelWidth: 80,
            msgTarget: 'side',
            allowBlank: false,
            emptyText: "请选择上传文件",
            blankText:"请选择上传文件",
            margin: '10,10,10,10',
            anchor: '100%',
            buttonText:'选择文件'
        }],
    buttonAlign:'center'

});


var impWin = Ext.create('Ext.window.Window', {
    title: '放箱',
    modal : true,
    items: [uploadForm],
    closeAction: 'hide',
    closeable: true,
    iconCls: 'modalWin',
    buttonAlign: 'center',
    buttons: [
        {text: '导入',handler: function() { impSubmit(); }},
        {text: '关闭', handler: function() { impWin.close(); }}
    ]
});

function impSubmit(){
    uploadForm.getForm().submit({
        url: '../bookingCargo/impPutBox',
        success: function(form, action) {
            var jsonResult = Ext.JSON.decode(action.response.responseText);
            if (jsonResult.success && jsonResult.code == "200") {
                Ext.Msg.alert("提示","放箱操作成功！",function(){
                    store.reload();
                    impWin.close();
                });
            }else{
                Ext.Msg.alert("提示","放箱操作失败，请检查文件内容是否正确！");
            }
        }
    });
}

function toImport(){
    impWin.show();
}